//
//  KFDAuxiliaryFunctionViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/4/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDAuxiliaryFunctionViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDSettingsManager.h"
#import "KFDClient.h"
#import "JTToast.h"
#import "KFDReachability.h"
#import <SDWebImage/SDImageCache.h>

@interface KFDAuxiliaryFunctionViewController ()

@property (strong,nonatomic) KFDSettingsManager* settingsManager;

@property (weak, nonatomic) IBOutlet UISwitch *autoAcceptPhotosOffWifiSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *autoPlayLiveOnWifiSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *forbidLiveOffWifiSwitch;

@end

@implementation KFDAuxiliaryFunctionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"辅助功能";
    self.settingsManager=[KFDSettingsManager sharedManager];
    self.autoAcceptPhotosOffWifiSwitch.on=self.settingsManager.auxiliaryFunctionSettings.isphoto;
    self.autoPlayLiveOnWifiSwitch.on=self.settingsManager.auxiliaryFunctionSettings.iswifi;
    self.forbidLiveOffWifiSwitch.on=self.settingsManager.auxiliaryFunctionSettings.deny_video;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    KFDSettingsManager* settingsManager=[KFDSettingsManager sharedManager];
    settingsManager.auxiliaryFunctionSettings.deny_video=self.forbidLiveOffWifiSwitch.isOn;
    settingsManager.auxiliaryFunctionSettings.iswifi=self.autoPlayLiveOnWifiSwitch.isOn;
    settingsManager.auxiliaryFunctionSettings.isphoto=self.autoAcceptPhotosOffWifiSwitch.isOn;
    [settingsManager updateAuxiliaryFunctionSettingsWithSuccessHandler:nil failureHandler:nil];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)autoAcceptPhotosOffWifiSwitch:(UISwitch*)sender {
    self.settingsManager.auxiliaryFunctionSettings.isphoto=sender.isOn;
}

- (IBAction)autoPlayLiveOnWifiSwitch:(UISwitch*)sender {
    self.settingsManager.auxiliaryFunctionSettings.iswifi=sender.isOn;
}

- (IBAction)forbidLiveOffWifiSwitch:(UISwitch *)sender {
    self.settingsManager.auxiliaryFunctionSettings.deny_video=sender.isOn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//------------------------------------------------------------------------
#pragma mark - Table view delegate
//------------------------------------------------------------------------
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 3:
        {
            UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"确定要清除聊天记录？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {
                KFDClient* client=[KFDClient sharedClient];
                [client clearChatLogWithSuccessHandler:^{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[JTToast makeText:@"清除成功"] showInView:self.view size:CGSizeMake(80, 40)];
                    });
                } failureHandler:^(KFDResponseBody *response) {
                    //                KFDReachability* reachability=[KFDReachability sharedReachability];
                    //                if (reachability.hostStatus==NotReachable) {
                    //                    [[JTToast makeText:@"请检查网络"] showInView:self.view size:CGSizeMake(100, 40)];
                    //                }
                }];
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:confirmAction];
            [self presentViewController:alertController animated:YES completion:nil];
            break;
        }
        case 4:
        {
            UIAlertController* alertController=[UIAlertController alertControllerWithTitle:nil message:@"确定要清除缓存数据？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            UIAlertAction* confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {
                SDImageCache *imageCache = [SDImageCache sharedImageCache];
                [imageCache clearMemory];
                [imageCache clearDisk];
                [[JTToast makeText:@"清除成功"] showInView:self.view size:CGSizeMake(80, 40)];
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:confirmAction];
            [self presentViewController:alertController animated:YES completion:nil];
            break;
        }
        default:
            break;
    }
}
//------------------------------------------------------------------------

@end
